import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MyDialogStudy extends StatefulWidget {
  const MyDialogStudy({Key? key}) : super(key: key);

  @override
  _MyDialogStudyState createState() => _MyDialogStudyState();
}

class _MyDialogStudyState extends State<MyDialogStudy> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DiaView(),
    );
  }
}

class DiaView extends StatefulWidget {
  const DiaView({Key? key}) : super(key: key);

  @override
  _DiaViewState createState() => _DiaViewState();
}

class _DiaViewState extends State<DiaView> {
  void _dialog() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('我是 dialog'),
            actions: [
                ElevatedButton(onPressed: (){}, child: Text('确定')),
             TextButton(onPressed: (){}, child: Text('取消'))
            ],
          );
        });
  }

  //单选dialog  确认，取消
  void _simple() async {
    var result = await showDialog(
       barrierDismissible: false,//点击灰色部分 是否自动取消
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text('确定要删除吗?'),
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  SimpleDialogOption(
                    child: Text('取消'),
                    onPressed: () {
                      Navigator.pop(context, '取消');
                    },
                  ),
                  SimpleDialogOption(
                    child: Text('确定'),
                    onPressed: () {
                      Navigator.pop(context, '确定');
                    },
                  )
                ],
              )
            ],
          );
        });

    print('result$result');
  }

  //底部按钮， 可以自定义
  void _bottomDialog() {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 210,
            child: Column(
              children: [
                ListTile(
                  title: Text('选择A'),
                  onTap: () {
                    Navigator.pop(context, "A");
                  },
                ),
                Divider(),
                ListTile(
                  title: Text('选择B'),
                  onTap: () {
                    Navigator.pop(context, "B");

                  },
                ),
                Divider(),
                ListTile(
                  title: Text('选择C'),
                  onTap: () {
                    Navigator.pop(context, "C");

                  },
                ),
              ],
            ),
          );
        }).then((value) {
      print('result$value');
    });
  }

  //第三方吐司toast
  void _toast(){

    Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );

  }

  //自定义dialog
  void _selfDialog(){


  }


  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
              onPressed: () {
                _dialog();
              },
              child: Text('alert弹出框-alertDialog')),
          ElevatedButton(
              onPressed: () {
                _simple();
              },
              child: Text('select弹出框-selectDialog')),
          ElevatedButton(
              onPressed: () {
                _bottomDialog();
              },
              child: Text('actionsheet底部弹出框-showmodalbottomsheet')),
          ElevatedButton(
              onPressed: () {
                _toast();
              }, child: Text('toast-flutter-toast第三方库')),
          ElevatedButton(
              onPressed: () {
                _selfDialog();
              }, child: Text('自定义dialog')),
        ],
      ),
    );
  }
}
